import 'package:flutter/material.dart';
import 'package:flutter_shopcar_test/model/index.dart';
import 'package:get/get.dart';

import 'shop_cart_page_logic.dart';

class ShopCartPagePage extends StatelessWidget {
  ShopCartPagePage({super.key});

  final logic = Get.put(ShopCartPageLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ShopCartPageLogic>(builder: (logic){
      return Scaffold(
        appBar: AppBar(
          title: const Text('购物车'),
          centerTitle: true,
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          actions: [
            IconButton(
              onPressed: logic.clearCart,
              icon: const Icon(Icons.delete),
            ),
          ],
        ),
        body: _buildContentWidget(),
      );
    });
  }

  Widget _buildContentWidget() {
    return Column(
      children: [
        _buildGoodsList(),
        _buildBottomWidget()
      ],
    );
  }

  Widget _buildGoodsList() {
    if(logic.cartGoodsList.isEmpty) {
      return const Expanded(child: Center(child: Text("暂无数据", style: TextStyle(fontSize: 14),)));
    }
    return Expanded(
      child: ListView.separated(
        itemBuilder: (context, index){
          CartModel model = logic.cartGoodsList[index];
          return _buildItemWidget(model);
        },
        separatorBuilder: (context, index){
          return Container(height: 10);
        },
        itemCount: logic.cartGoodsList.length,
        shrinkWrap: true,
        padding: const EdgeInsets.all(12),
      ),
    );
  }

  Widget _buildItemWidget(CartModel model) {
    return Container(
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8)
      ),
      child: Row(
        children: [
          Container(
            color: const Color(0xFFdddddd),
            width: 100,
            height: 100,
            child: const Icon(Icons.image),
          ),
          const SizedBox(width: 12,),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(model.shopCartName ?? '', style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600),),
                _buildSizedBoxWidget(),
                Text("商品编码：${model.shopCartCode ?? ''}", style: const TextStyle(fontSize: 14),),
                _buildSizedBoxWidget(),
                _buildCountWidget(model)
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildCountWidget(CartModel model) {
    return Row(
      children: [
        Text("￥${model.shopCartPrice}", style: const TextStyle(fontSize: 14, color: Color(0xFF666666)),),
        Expanded(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              InkWell(
                onTap: () => logic.addCount(model),
                child: _buildContainerWidget(
                  widget: const Text("+", style: TextStyle(fontSize: 16),),
                ),
              ),
              const SizedBox(width: 6,),
              _buildContainerWidget(
                widget: Text("${model.shopCartQty}", style: const TextStyle(fontSize: 16),),
              ),
              const SizedBox(width: 6,),
              InkWell(
                onTap: () => logic.subCount(model),
                child: _buildContainerWidget(
                  widget: const Text("-", style: TextStyle(fontSize: 16),),
                ),
              ),
            ],
          ),
        )
      ],
    );
  }

  Widget _buildContainerWidget({required Widget widget}) {
    return Container(
      width: 30,
      height: 30,
      color: const Color(0xFFDDDDDD),
      child: Center(child: widget,),
    );
  }

  Widget _buildBottomWidget() {
    return Container(
      color: Colors.white,
      padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 14),
      child: _buildButtonWidget(),
    );
  }

  Widget _buildButtonWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text("共计：${logic.totalPrice()}", style: const TextStyle(fontSize: 14),),
        InkWell(
          onTap: () {
            if(logic.cartGoodsList.isNotEmpty) {
              bottomDialog();
            }
          },
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 10),
            decoration: BoxDecoration(
                color: Colors.blue,
                borderRadius: BorderRadius.circular(4)
            ),
            child: const Text('结算', style: TextStyle(color: Colors.white),),
          ),
        ),
      ],
    );
  }

  void bottomDialog() {
    Get.bottomSheet(
      backgroundColor: Colors.white,
      // isDismissible: false,
      Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          const Padding(
            padding: EdgeInsets.symmetric(vertical: 12),
            child: Text("请选择支付方式", style: TextStyle(fontSize: 18),),
          ),
          Wrap(
            children: logic.payWay.map((e) {
              return ListTile(
                title: Text(e, style: const TextStyle(fontSize: 16),),
                onTap: () => logic.submitOrder(e),
              );
            }).toList(),
          ),
        ],
      ),
    );
  }

  Widget _buildSizedBoxWidget() {
    return const SizedBox(height: 10);
  }

}
